import { spawn } from 'cross-spawn';
import request from '../utils/request';

const baseUrl =
  'http://pipeline.ebonex.io/api/cms/publish/content?chunkId=24&env=4';
export const getConfig = async () => {
  console.log(`请求配置信息：${baseUrl}`);
  const res = await request(baseUrl);
  const data = res?.data;
  if (data) {
    return data?.jenkins || {};
  }
  return null;
};

//['rev-parse', '--abbrev-ref', 'HEAD']
export function stdoutResult(
  command: string,
  options: Array<string>,
): Promise<string> {
  return new Promise((resolve, reject) => {
    const child = spawn(command, options, {
      cwd: process.cwd(),
    });

    let str = '';
    child.stdout.on('data', (data) => {
      str += data.toString();
    });
    child.stderr.on('data', (data) => {
      reject(data.trim());
    });
    child.stdout.on('close', () => {
      resolve(str);
    });
  });
}
